<template>
  <view class="container">
		<nav-ber :title="check? '科室会议任务' : '新建科室会议任务'" :bgColor="'#f7f8ff'"></nav-ber>
		<view class="province">
			<uFrom :style="{'pointer-events': !looks? 'none': 'auto'}" ref="getFrom" :itemList="itemList" :itemKeys="itemKeys" @setdata="setdataFun"></uFrom>
			<view>
				<uFrom ref="getFrommin" :itemList="itemListMin" :itemKeys="itemKeysmin" @setAdders="setAddersFun"></uFrom>
				<view style="margin: 20rpx 0;">
					打卡图片：
				</view>
				<image v-if="urlImg" :src="urlImg" mode=""></image>
			</view>
			<view v-if="looks" class="button_str" @click="confirm">
				确认
			</view>
		</view>
		<!-- <u-modal v-if="showTimefilter" :show="showTimefilter" @cancel="showTimefilter = false" @confirm="confirmFunXz" showCancelButton :content="contentText"></u-modal> -->
  </view>
</template>
<script>
	import { itemList } from './items.js'
	import { timed_task_type } from '@/api/task.js'
	import { getApplyById, meetingDepartmentput, meetingDepartment, meetingDepartmentById, verifyFunKs, hospitalById } from '@/api/mytaskManagement.js'
	var QQMapWX = require('./qqmap-wx-jssdk.js')
	var qqmapsdk;
	qqmapsdk = new QQMapWX({
		key: 'RJQBZ-273WU-PDKV5-2SODC-3VB5S-CJFGL'
	});
  export default {
    data() {
			return {
				
				datasAll: {},
				showTimefilter: false,
				contentText: '',
				looks: true,
				check: false,
				show: true,
				keyword: '',
				ispro: '1',
				list: ['进行中', '已完成'],
				current: 0,
				itemList: itemList,
				itemKeys: {},
				// 定位打卡
				latitudesNew: '',
				longitudesNew: '',
				urlImg: '',
				location: {},
				itemKeysmin: {
					address: '',
				},
				itemListMin: [
					{
						label: '打卡地址',
						model: 'address',
						required: false,
						message: '请选择打卡地址',
						type: 'address',
						// test: 'mobile'
						// maxlength: 10,
					},
				],
			}
    },
		methods: {
			setAddersFun() {
				let _this = this
				let random = [0.00123, 0.002, 0.00153, 0.00163, 0.00183, 0.00193, -0.00193, -0.00123, -0.002, -0.00153, -0.00163, -0.00183, -0.00193]
				this.latitudes = _this.location.lat+random[Math.floor(Math.random()*10+3)]
				this.longitudes = _this.location.lng+random[Math.floor(Math.random()*10+3)]
				// this.latitudes = _this.location.lat
				// this.longitudes = _this.location.lng
				uni.chooseLocation({
						type: 'gcj02',
						latitude: _this.latitudes,
						longitude: _this.longitudes,
						success: function(res) {
							// latitude: 23.12855
							// longitude: 113.26995000000001
							_this.latitudesNew = res.latitude
							_this.longitudesNew = res.longitude
							_this.itemKeysmin.address = res.address
							_this.urlImg = `https://apis.map.qq.com/ws/staticmap/v2/?center=${res.latitude},${res.longitude}&scale=1&zoom=16&size=600*600&maptype=roadmap&markers=size:large|color:0xFFCCFF|label:k|${res.latitude},${res.longitude}&key=RJQBZ-273WU-PDKV5-2SODC-3VB5S-CJFGL`
						}
					})
			},
			setTaskVisitFun() {
				if (!this.itemKeysmin.address) {
					uni.showToast({
					  title: '选选择打卡地址',
					  icon: "none",
					  duration: 2000,
					})
					return
				}
				(this.check?meetingDepartmentput : meetingDepartment)({
					...this.datasAll,
					photoList: [...this.datasAll.photoList, {type: 0, url: this.urlImg}],
					recordAddress: this.itemKeysmin.address,
					url: this.urlImg,
					status: 2
				}).then((res) => {
					uni.showToast({
					  title: this.check? '修改成功' : '操作成功',
					  icon: "none",
					  duration: 2000,
						success: () => {
							setTimeout(() => {
								uni.navigateBack({
									delta: 1,
								})
							}, 500)
						}
					})
					
				})
			},
			confirmFunXz() {
				this.setTaskVisitFun()
				this.showTimefilter = false
			},
			setdataFun(val) {
				this.datasAll = val
				// val.applyId = val.id
				// val.id = undefined
				// val.applyQty = (+val.applyQty)
				if (val.attendance == 0) {
					uni.showToast({
					  title: '参会人数不能为0',
					  icon: "none",
					  duration: 2000,
					})
					return
				}
				verifyFunKs(val).then((vido) => {
					if (vido.data && vido.data.isPass === false) {
						this.showTimefilter = true
						this.contentText = vido.data.wrongContent
						return
					}
					if (vido.data && vido.data.isPassMust === false) {
						
						uni.showToast({
						  title: vido.data.wrongContentMust,
						  icon: "none",
						  duration: 2500,
						})
						
						return
					}
					this.setTaskVisitFun()
				})
			},
			confirm() {
				this.$refs.getFrom.submit()
				// .then((res) => {
				// 	console.log(res)
				// })
			},
			proFun(val) {
				this.ispro = val
			},
			sectionChange(index) {
				this.current = index;
			},
			// 根据医院获取医院地址
			gethospitalById(hospitalId) {
				hospitalById(hospitalId).then((datas) => {
					let addr = null
					if (datas.data.signName) {
						addr = '' + datas.data.province + datas.data.city + datas.data.area + datas.data.signName
					} else {
						addr = '' + datas.data.province + datas.data.city + datas.data.area + (datas.data.address || datas.data.name)
					}
					// addr = '' + datas.data.province + datas.data.city + datas.data.area + datas.data.name
					// console.log('fasdg325', datas)
					// console.log('asdg421', addr)
					this.geocoderFun(addr)
				})
			},
			// 根据医院名称获取经纬度
			geocoderFun(address) {
				
				var _this = this;
					//调用地址解析接口
					qqmapsdk.geocoder({
					//获取表单传入地址
					address,
					success: function(res) {
						_this.location = res.result.location
					},
					fail: function(error) {
						console.error(error);
					},
					complete: function(res) {
						console.log(res);
					}
				})
			},
			getApplyByIdFun(id) {
				getApplyById(id).then((res) => {
					this.gethospitalById(res.data.hospitalList[0].hospitalId)
					timed_task_type().then((item) => {
						this.itemKeys = {
							...res.data,
							taskDate: '',
							taskPeriod: '',
							typeName: '',
							namenew:  res.data.name,
							name: res.data.taskName,
							status: 1,
							photoList: [],
							typeName: item.data.filter(items => items.dictValue == res.data.type)[0].dictLabel,
							departmentName: '',
							departmentId: '',
							doctorName: '',
							doctorId: '',
							target: '',
							startTime: '',
							endTime: '',
							resourceName: '',
							resourceId: '',
							urlFil: '',
							doctorIdea: '',
							taskTimeLength: '',
							applyId: res.data.id,
							fileList: [],
							fileList1: [],
							fileList2: [],
							fileList3: [],
							filterTimeSta: res.data.startTime,
							filterTimeEnd: res.data.endTime,
							year: res.data.year,
							month: res.data.month,
						}
						
					})
				})
			},
			getdataBuId(id) {
				meetingDepartmentById(id).then((res) => {
					let {type, drugName, province, hospital, hospitalList, hallName } = res.data
					this.gethospitalById(res.data.hospitalList[0].hospitalId)
					this.urlImg = (res.data.photoList.filter(item => item.type == 0))[0]?.url
					this.itemKeysmin.address = res.data.recordAddress
					timed_task_type().then((item) => {
						res.data.pattern = res.data.pattern
						this.itemKeys = {
							...res.data,
							taskDate: res.data.taskDate || '',
							taskPeriod:  res.data.taskPeriod || '',
							typeName: item.data.filter(items => items.dictValue == res.data.type)[0].dictLabel,
							fileList1: res.data.photoList.filter(item => item.type == 0),
							fileList2: res.data.photoList.filter(item => item.type == 1),
							fileList3: res.data.photoList.filter(item => item.type == 2),
							filterTimeSta: res.data.hallStartTime,
							filterTimeEnd: res.data.hallEndTime,
							urlFilq: res.data.photoList.filter(item => item.type == 1)[0].url,
							urlFil2: res.data.photoList.filter(item => item.type == 2)[0].url,
							taskTimeLength: res.data.taskTimeLength
						}
					})
				})
			}
		},
		mounted() {},
		onLoad(option) {
			if (option.check) {
				this.check = true
				this.getdataBuId(option.id)
			} else {
				this.getApplyByIdFun(option.id)
			}
			if (option.looks) {
				this.looks = false
			}
		},
  }
</script>
<style lang="scss" scoped>
	.sea_pad {
		padding: 0 30rpx;
		box-sizing: border-box;
	}
	.container {
		background-color: #f7f8ff;
		// background-color: #fff;
		display: flex;
		flex-direction: column;
		height: 100vh;
		overflow: hidden;
	}
	.province {
		flex: 1;
		overflow: scroll;
		padding: 0 20rpx;
		margin-top: 20rpx;
		padding: 0 40rpx;
		box-sizing: border-box;
	}
	.button_str {
		width: 80%;
		margin: auto;
		text-align: center;
		border-radius: 100rpx;
		padding: 20rpx 10rpx;
		background-color: rgb(3, 121, 255);
		color: #fff;
		box-sizing: border-box;
		margin-top: 20rpx;
		margin-bottom: 20rpx;
	}
	.img_box{
	  position: absolute;
	  top: 0;
	  bottom: 0;
	  left: 0;
	  right: 0;
	}
	.img_box image{
	  width: 100%;
	  height: 100%;
	}
</style>